package s01;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ClassName FileUploadServer
 * @Description 服务端实现
 * @Author Li
 * @Date 2022/8/13 10:17
 * @ModifyDate 2022/8/13 10:17
 * @Version 1.0
 */
public class FileUploadServer {
    public static void main(String[] args) throws IOException {
        System.out.println("服务器启动...");
        // 1.创建服务端ServerSocket
        ServerSocket server = new ServerSocket(6666);
        // 2.建立连接
        Socket socket = server.accept();
        // 3.创建流对象
        // 3.1获取输入流，读取文件数据
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        // 3.2创建输出流，保存到本地
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\output.png"));
        // 4.读写数据
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        // 5.关闭资源
        bos.close();
        bis.close();
        server.close();
        System.out.println("文件上传成功！！！");
    }
}
